package jobs;


import org.apache.commons.mail.Email;

import play.jobs.Job;
import play.jobs.On;
import play.libs.Mail;
import util.EmailHelper;

@On("0 15 10 * * ? ")
public class SendOrdersSummaryMailJobs extends Job<String> {

	@Override
	public String doJobWithResult() {

		Email email = EmailHelper.buildOrderListEmail();
		if(email != null)
		{
			Mail.send(email);
		}
		return null;
	}
	
	/*public static void sendEmail()
	{
		try {
			String messageBody = buildMessageBody();
			if (messageBody != null && messageBody.trim().length() > 0) {
				SimpleEmail email = new SimpleEmail();
				email.setFrom(SettingsReader.getProperty(
						Constants.EMAIL_SEND_AS, "hxu@sybase.com"));
				String temp = SettingsReader.getProperty(
						Constants.EMAIL_SEND_TO, "hao.xu@sybase.com");
				for (String item : temp.split(",")) {
					email.addTo(item);
				}

				temp = SettingsReader.getProperty(Constants.EMAIL_SEND_CC,
						"keli@sybase.com");
				for (String item : temp.split(",")) {
					email.addCc(item);
				}
				email.setCharset("utf-8");
				email.setSubject("Lunch Orders Summary, generated at [ "
						+ (new Date().toString()) + " ]");

				email.setMsg(messageBody);

				Mail.send(email);
			}
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static String buildMessageBody() {
		StringBuilder sb = new StringBuilder(
				"\n===========================\n\n\n");
		List<OrderItem> orders = OrderDAO.getTodayOrders();
		if (orders == null || orders.size() <= 0) {
			sb.append("\n今天没有预定\n\n\n");
		}
		else
		{
			Map<String, List<OrderItem>> foodGroups = new HashMap<String, List<OrderItem>>();
			for (OrderItem item : orders) {
				if (!foodGroups.containsKey(item.food.name)) {
					foodGroups.put(item.food.name, new ArrayList<OrderItem>());
				}
				foodGroups.get(item.food.name).add(item);
			}

			
			for (String key : foodGroups.keySet()) {
				sb.append(key);
				sb.append(",");
				sb.append(foodGroups.get(key).size());
				sb.append("\n");
			}
			sb.append("共 [ ");
			sb.append(orders.size());
			sb.append(" ] 份");
			sb.append("\n\n===========================\n\n\n");
			for (OrderItem o : orders) {
				sb.append(o.user.userName);
				sb.append("\t: ");
				sb.append(o.food.name);
				sb.append("\t; ");
				sb.append(o.add_date);
				sb.append("\n");
			}
		}
		sb.append("\n===========================\n");
		sb.append("分机号:7464 7417 7468\n");
		return sb.toString();
	}*/
}
